package com.cyedu.tsapp.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cyedu.tsapp.entity.AppDefault;
import com.cyedu.tsapp.entity.AppInfo;
import com.cyedu.tsapp.entity.AppRecommended;
import com.cyedu.tsapp.entity.Result;
import com.cyedu.tsapp.entity.pojo.DefaultPojo;
import com.cyedu.tsapp.entity.vo.DefaultVo;
import com.cyedu.tsapp.entity.vo.RecommendVo;
import com.cyedu.tsapp.service.IAppDefaultService;
import com.cyedu.tsapp.service.IAppInfoService;
import com.cyedu.tsapp.service.IAppRecommendedService;
import com.cyedu.tsapp.utils.ResultUtil;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.xmlbeans.impl.xb.xsdschema.AppinfoDocument;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Garne
 * @since 2021-06-26
 */
@RestController
@RequestMapping("/appDefault")
public class AppDefaultController {

    @Autowired
    IAppDefaultService defaultService;

    @Autowired
    IAppRecommendedService recommendedService;

    @Autowired
    IAppInfoService appInfoService;

    @ApiOperation(value = "获取空间默认场景", notes = "")
    @RequestMapping(value = "/getDefGroupByZone", method = RequestMethod.GET)
    public Result getDefGroupByZone(@RequestParam(value = "zone") String zone) {
        if (StringUtils.isBlank(zone)) {
            return ResultUtil.fail("the param 'zone' is null");
        }

        QueryWrapper<AppDefault> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("`zone`", zone);
        List<AppDefault> appDefaults = defaultService.list(queryWrapper);

        DefaultVo defaultVo = new DefaultVo();
        defaultVo.setName(zone);

        if (null != appDefaults && appDefaults.size() > 0) {
            // 查询默认推荐
            List<RecommendVo> recommendVoList = new ArrayList<>();
            for (int i = 0; i < appDefaults.size(); i++) {
                RecommendVo recommendVo = new RecommendVo();
                AppDefault ad = appDefaults.get(i);
                recommendVo.setId(ad.getId());
                QueryWrapper<AppRecommended> recommendWrapper = new QueryWrapper<>();
                recommendWrapper.eq("`group`", ad.getGroupName());
                List<AppRecommended> ars = recommendedService.list(recommendWrapper);
                if (null != ars && ars.size() > 0) {
                    // 查询推荐应用信息
                    List<AppInfo> appInfos = new ArrayList<>();
                    for (int j = 0; j < ars.size(); j++) {
                        AppRecommended ar = ars.get(j);
                        QueryWrapper<AppInfo> infoQueryWrapper = new QueryWrapper<>();
                        infoQueryWrapper.eq("app_name", ar.getAppName());
                        AppInfo info = appInfoService.getOne(infoQueryWrapper);
                        if(info != null){
                            appInfos.add(info);
                        }
                    }
                    recommendVo.setAppInfoList(appInfos);
                }
                recommendVo.setName(ad.getGroupName());
                recommendVoList.add(recommendVo);
            }
            defaultVo.setRecommendList(recommendVoList);

        }

        return ResultUtil.success(defaultVo);
    }

    @ApiOperation(value = "保存推荐应用场景到空间", notes = "")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(@RequestBody DefaultPojo param) {
        if (null == param) {
            return ResultUtil.fail("the param is null");
        }
        QueryWrapper<AppDefault> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("`zone`", param.getZone());
        defaultService.remove(queryWrapper);
        if (null != param.getGroupNames() && param.getGroupNames().size() != 0) {
            for (String groupName : param.getGroupNames()) {
                AppDefault ad = new AppDefault();
                ad.setZone(param.getZone());
                ad.setGroupName(groupName);
                defaultService.save(ad);
            }
        }
        return ResultUtil.success("操作成功");
    }
}
